#include "mydatabase.h"
#include <QDebug>
#include <QSqlError>
MyDatabase::MyDatabase()
{
    if(ConnectDB())
    {
        qDebug()<<"success to connect DB";
    }
    else
    {
        qDebug()<<"filed to connect DB";
    }
    CreateTable();
}

MyDatabase::~MyDatabase()
{
    if(db.isOpen())
    {
        db.close();
    }
}


//增加


//患者登陆表
void MyDatabase::insertDataPatient(const QMap<QString,QString>& data)
{
    QString sql = "INSERT INTO patientusr VALUES(?,?,?,?,?,?,?);";
    QSqlQuery sq;
    //预处理
    sq.prepare(sql);
    //绑定参数
    sq.addBindValue(data.value("id").toStdString().c_str());
    sq.addBindValue(data.value("name").toStdString().c_str());
    sq.addBindValue(data.value("sex").toStdString().c_str());
    sq.addBindValue(data.value("age").toInt());
    sq.addBindValue(data.value("phone").toStdString().c_str());
    sq.addBindValue(data.value("address").toStdString().c_str());
    sq.addBindValue(data.value("passward").toStdString().c_str());
    //执行绑定后的SQL语句
    if(sq.exec())
    {
        qDebug()<<"Patientuser insert succeed";
    }
    else
    {
        qDebug()<<"Patientuser insert default";
    }
}
//医生登陆表
void MyDatabase::insertDataDoctor(const QMap<QString,QString>&data)
{
    QString sql = "INSERT INTO doctorusr VALUES(?,?,?,?,?,?,?,?,?);";
    QSqlQuery sq;
    //预处理
    sq.prepare(sql);
    sq.addBindValue(data.value("id").toStdString().c_str());
    sq.addBindValue(data.value("name").toStdString().c_str());
    sq.addBindValue(data.value("sex").toStdString().c_str());
    sq.addBindValue(data.value("age").toInt());
    sq.addBindValue(data.value("phone").toStdString().c_str());
    sq.addBindValue(data.value("adddress").toStdString().c_str());
    sq.addBindValue(data.value("passward").toStdString().c_str());
    sq.addBindValue(data.value("departmentname").toStdString().c_str());
    sq.addBindValue(data.value("departmentid").toStdString().c_str());
    if(sq.exec())
    {
        qDebug()<<"Doctoruser insert succeed";
    }
    else
    {
        qDebug()<<"Doctoruser insert default";
    }
}
//科室表
void MyDatabase::insertDataDepartment(const QMap<QString,QString>&data)
{
    QString sql = "INSERT INTO department VALUES(?,?,?,?);";
    QSqlQuery sq;
    //预处理
    sq.prepare(sql);
    sq.addBindValue(data.value("departmentname").toStdString().c_str());
    sq.addBindValue(data.value("departmentid").toStdString().c_str());
    sq.addBindValue(data.value("doctorname").toStdString().c_str());
    sq.addBindValue(data.value("doctorid").toStdString().c_str());
    if(sq.exec())
    {
        qDebug()<<"Department insert succeed";
    }
    else
    {
        qDebug()<<"Department insert default";
    }

}
//健康评估表
void MyDatabase::insertDataHealthassessment(const QMap<QString, QString> &data)
{
    QString sql = "INSERT INTO healthassessment VALUES(?,?,?,?,?,?);";
    QSqlQuery sq;
    //预处理
    sq.prepare(sql);
    sq.addBindValue(data.value("patientid").toStdString().c_str());
    sq.addBindValue(data.value("patientname").toStdString().c_str());
    sq.addBindValue(data.value("height").toStdString().c_str());
    sq.addBindValue(data.value("weight").toStdString().c_str());
    sq.addBindValue(data.value("bloodpressure").toStdString().c_str());
    sq.addBindValue(data.value("heartrate").toStdString().c_str());
    if(sq.exec())
    {
        qDebug()<<"healthassessment insert succeed";
    }
    else
    {
        qDebug()<<"healthassessment insert default";
    }
}
//病例表
void MyDatabase::insertDataMedicialrec(const QMap<QString, QString> &data)
{
    QString sql = "INSERT INTO medicialrec VALUES(?,?,?,?,?);";
    qDebug()<<"here";
    QSqlQuery sq;
    //预处理
    sq.prepare(sql);
    sq.addBindValue(data.value("patientid").toStdString().c_str());
    sq.addBindValue(data.value("patientname").toStdString().c_str());
    sq.addBindValue(data.value("doctorname").toStdString().c_str());
    sq.addBindValue(data.value("medic").toStdString().c_str());
    sq.addBindValue(data.value("date").toStdString().c_str());
    if(sq.exec())
    {
        qDebug()<<"medicialrec insert succeed";
    }
    else
    {
        qDebug()<<"medicialrec insert default";
        qWarning() << "Query execution failed:" << sq.lastError().text();
    }
}
//处方表
void MyDatabase::insertDataPrescription(const QMap<QString, QString> &data)
{
    QString sql = "INSERT INTO prescription VALUES(?,?,?,?,?,?);";
    QSqlQuery sq;
    //预处理
    sq.prepare(sql);
    sq.addBindValue(data.value("doctorid").toStdString().c_str());
    sq.addBindValue(data.value("doctorname").toStdString().c_str());
    sq.addBindValue(data.value("patientid").toStdString().c_str());
    sq.addBindValue(data.value("patientname").toStdString().c_str());
    sq.addBindValue(data.value("prescr").toStdString().c_str());
    sq.addBindValue(data.value("date").toStdString().c_str());
    if(sq.exec())
    {
        qDebug()<<"prescription insert succeed";
    }
    else
    {
        qDebug()<<"prescription insert default";
        qWarning() << "Query execution failed:" << sq.lastError().text();
    }
}
//挂号表
void MyDatabase::insertDataRegistration(const QMap<QString, QString> &data)
{
    QString sql = "INSERT INTO registration(patientname,patientid,doctorname,departmentname,date) VALUES(?,?,?,?,?);";
    QSqlQuery sq;
    //预处理
    sq.prepare(sql);
    sq.addBindValue(data.value("patientname").toStdString().c_str());
    sq.addBindValue(data.value("patientid").toStdString().c_str());
    sq.addBindValue(data.value("doctorname").toStdString().c_str());
    sq.addBindValue(data.value("departmentname").toStdString().c_str());
    sq.addBindValue(data.value("date").toStdString().c_str());
    if(sq.exec())
    {
        qDebug()<<"registration insert succeed";
    }
    else
    {
        qDebug()<<"registration insert default";
    }
}



//删除
void MyDatabase::deleteDataPatient(const QMap<QString,QString>&data)
{
    QString sql = "DELETE FROM paientusr WHERE name =? AND id=?";
    QSqlQuery sq;

    sq.prepare(sql);
    sq.addBindValue(data.value("name").toStdString().c_str());
    sq.addBindValue(data.value("id").toStdString().c_str());
    if(sq.exec())
    {
        qDebug()<<"Patient delete succeed";
    }
    else
    {
        qDebug()<<"Patient delete default";
    }
}

void MyDatabase::deleteDataDoctor(const QMap<QString,QString>&data)
{
    QString sql = "DELETE FROM doctorusr WHERE name =? AND id=?";
    QSqlQuery sq;

    sq.prepare(sql);
    sq.addBindValue(data.value("name").toStdString().c_str());
    sq.addBindValue(data.value("id").toStdString().c_str());
    if(sq.exec())
    {
        qDebug()<<"Doctor delete succeed";
    }
    else
    {
        qDebug()<<"Doctor delete default";
    }
}

void MyDatabase::deleteDataDepartment(const QMap<QString,QString>&data)
{
    QString sql = "DELETE FROM department WHERE departmentname =? AND departmentid=?";
    QSqlQuery sq;

    sq.prepare(sql);
    sq.addBindValue(data.value("departmentname").toStdString().c_str());
    sq.addBindValue(data.value("departmentid").toStdString().c_str());
    if(sq.exec())
    {
        qDebug()<<"Department delete succeed";
    }
    else
    {
        qDebug()<<"Deparment delete default";
    }
}

void MyDatabase::deleteDataHealthassessment(const QMap<QString, QString> &data)
{
    QString sql = "DELETE FROM healthassessment WHERE departmentname =? AND departmentid=?";
    QSqlQuery sq;

    sq.prepare(sql);
    sq.addBindValue(data.value("patientid").toStdString().c_str());
    sq.addBindValue(data.value("patientname").toStdString().c_str());
    if(sq.exec())
    {
        qDebug()<<"Healthassessment delete succeed";
    }
    else
    {
        qDebug()<<"Healthassessment delete default";
    }
}

void MyDatabase::deleteDataMedicialrec(const QMap<QString, QString> &data)
{
    QString sql = "DELETE FROM medicialrec WHERE patientid =? AND patientname =?";
    QSqlQuery sq;

    sq.prepare(sql);
    sq.addBindValue(data.value("patientid").toStdString().c_str());
    sq.addBindValue(data.value("patientname").toStdString().c_str());
    if(sq.exec())
    {
        qDebug()<<"medicialrec delete succeed";
    }
    else
    {
        qDebug()<<"medicialrec delete default";
    }
}

void MyDatabase::deleteDataPrescription(const QMap<QString, QString> &data)
{
    QString sql = "DELETE FROM prescription WHERE patientid =? AND patientname =?";
    QSqlQuery sq;

    sq.prepare(sql);
    sq.addBindValue(data.value("patientid").toStdString().c_str());
    sq.addBindValue(data.value("patientname").toStdString().c_str());
    if(sq.exec())
    {
        qDebug()<<"prescription delete succeed";
    }
    else
    {
        qDebug()<<"prescription delete default";
    }
}

void MyDatabase::deleteDataRegistration(const QMap<QString, QString> &data)
{
    QString sql = "DELETE FROM registration WHERE patientid =? AND patientname =?";
    QSqlQuery sq;

    sq.prepare(sql);
    sq.addBindValue(data.value("patientid").toStdString().c_str());
    sq.addBindValue(data.value("patientname").toStdString().c_str());
    if(sq.exec())
    {
        qDebug()<<"registration delete succeed";
    }
    else
    {
        qDebug()<<"registration delete default";
    }
}




//修改
void MyDatabase::modifiDataPatient(const QMap<QString, QString>& data)
{
    // 修正字段顺序和拼写错误
    QString sql = "UPDATE patientusr SET name = ?, sex = ?, age = ?, phone = ?, address = ?, passward = ? WHERE id = ?;";
    QSqlQuery sq;

    sq.prepare(sql);

    // 确保绑定值按顺序正确
    sq.addBindValue(data.value("name"));           // name
    sq.addBindValue(data.value("sex"));            // sex
    sq.addBindValue(data.value("age").toInt());    // age
    sq.addBindValue(data.value("phone"));          // phone
    sq.addBindValue(data.value("address"));        // address
    sq.addBindValue(data.value("passward"));       // passward
    sq.addBindValue(data.value("id").toInt());     // id

    // 执行更新语句并输出详细的错误信息
    if (sq.exec()) {
        qDebug() << "Patient modification succeeded";
    } else {
        qDebug() << "Patient modification failed:" << sq.lastError().text();
    }
}

void MyDatabase::modifiDataDoctor(const QMap<QString,QString>&data)
{
    QString sql ="UPDATE doctorusr SET name =?,id=?,sex=?,age=?,phone=?,address=?,passward=?,departmentname=?,departmentid=? where id=?;";
    QSqlQuery sq;

    sq.prepare(sql);
    sq.addBindValue(data.value("name").toStdString().c_str());
    sq.addBindValue(data.value("id").toStdString().c_str());
    sq.addBindValue(data.value("sex").toStdString().c_str());
    sq.addBindValue(data.value("age").toInt());
    sq.addBindValue(data.value("phone").toStdString().c_str());
    sq.addBindValue(data.value("address").toStdString().c_str());
    sq.addBindValue(data.value("passward").toStdString().c_str());
    sq.addBindValue(data.value("departmentname").toStdString().c_str());
    sq.addBindValue(data.value("departmentid").toStdString().c_str());
    sq.addBindValue(data.value("id").toStdString().c_str());
    if(sq.exec())
    {
        qDebug()<<"Doctor modifi succeed";
    }
    else
    {
        qDebug()<<"Doctor modifi default";
    }
}

void MyDatabase::modifiDataHealthassessment(const QMap<QString, QString> &data)
{
    QString sql ="UPDATE healthassessment SET height =?,weight=?,bloodpressure=?, heartrate =? where patientid=? AND patientname =?";
    QSqlQuery sq;
    sq.prepare(sql);
    sq.addBindValue(data.value("height").toInt());
    sq.addBindValue(data.value("weight").toInt());
    sq.addBindValue(data.value("bloodpressure").toInt());
    sq.addBindValue(data.value("heartrate").toInt());
    sq.addBindValue(data.value("patientid").toStdString().c_str());
    sq.addBindValue(data.value("patientname").toStdString().c_str());
    if(sq.exec())
    {
        qDebug()<<"Healthassessment modifi succeed";
    }
    else
    {
        qDebug()<<"Healthassessment modifi default";
    }
}

void MyDatabase::modifiDataMedicialrec(const QMap<QString, QString> &data)
{
    QString sql ="UPDATE medicialrec SET medic=? WHERE patientid=? and patientname=?";
    QSqlQuery sq;
    sq.prepare(sql);
    sq.addBindValue(data.value("medic").toStdString().c_str());
    sq.addBindValue(data.value("patientid").toStdString().c_str());
    sq.addBindValue(data.value("patientname").toStdString().c_str());
    if(sq.exec())
    {
        qDebug()<<"Medicialrec modifi succeed";
    }
    else
    {
        qDebug()<<"Medicialrec modifi default";
    }
}

void MyDatabase::modifiDataPrescription(const QMap<QString, QString> &data)
{
    QString sql ="UPDATE prescription SET prescr=? WHERE patientid=? and patientname=?";
    QSqlQuery sq;
    sq.prepare(sql);
    sq.addBindValue(data.value("prescr").toStdString().c_str());
    sq.addBindValue(data.value("patientid").toStdString().c_str());
    sq.addBindValue(data.value("patientname").toStdString().c_str());
    if(sq.exec())
    {
        qDebug()<<"Prescription modifi succeed";
    }
    else
    {
        qDebug()<<"Prescription modifi default";
    }
}

void MyDatabase::modifiDataRegistration(const QMap<QString, QString> &data)
{
    QString sql ="UPDATE registraton SET departmentname =?,doctorname=?,doctorid =? where patientname=? and patientid =?";
    QSqlQuery sq;

    sq.addBindValue(data.value("departmentname").toStdString().c_str());
    sq.addBindValue(data.value("doctorname").toStdString().c_str());
    sq.addBindValue(data.value("doctorid").toStdString().c_str());
    sq.addBindValue(data.value("patientname").toStdString().c_str());
    sq.addBindValue(data.value("patientid").toStdString().c_str());
    if(sq.exec())
    {
        qDebug()<<"Department modifi succeed";
    }
    else
    {
        qDebug()<<"Department modifi default";
    }
}

void MyDatabase::modifiDataDepartment(const QMap<QString,QString>&data)
{
    QString sql ="UPDATE department SET departmentname =?,departmentid=?,doctorname=?,doctorid =? where doctorname=? and doctorid =?";
    QSqlQuery sq;

    sq.addBindValue(data.value("departmentname").toStdString().c_str());
    sq.addBindValue(data.value("departmentid").toStdString().c_str());
    sq.addBindValue(data.value("doctorname").toStdString().c_str());
    sq.addBindValue(data.value("doctorid").toStdString().c_str());
    sq.addBindValue(data.value("doctorname").toStdString().c_str());
    sq.addBindValue(data.value("doctorid").toStdString().c_str());
    if(sq.exec())
    {
        qDebug()<<"Department modifi succeed";
    }
    else
    {
        qDebug()<<"Department modifi default";
    }
}




//查找

//原来版本
// QMap<QString,QString>* MyDatabase::checkDataPatient(const QMap<QString,QString>&data)
// {
//     QString sql ="select * from patientusr where name =? and id=?;";
//     QSqlQuery sq;
//     sq.prepare(sql);
//     sq.addBindValue(data.value("name").toStdString().c_str());
//     sq.addBindValue(data.value("id").toStdString().c_str());
//     //QMap<QString,QString> data [3] 和 QList<QMap<QString,QString>> data 的区别
//     patientdata =new QMap<QString,QString>[sq.size()];
//     int count=0;
//     //遍历查询语句的结果
//     while(sq.next())
//     {
//         patientdata[count].insert("id",sq.value("id").toString());
//         patientdata[count].insert("name",sq.value("name").toString());
//         patientdata[count].insert("sex",sq.value("sex").toString());
//         patientdata[count].insert("age",sq.value("age").toString());     //可能存在错误
//         patientdata[count].insert("phone",sq.value("phone").toString());
//         patientdata[count].insert("address",sq.value("address").toString());
//         patientdata[count].insert("passward",sq.value("passward").toString());
//         patientdata[count].insert("department",sq.value("departemnt").toString());
//         patientdata[count].insert("doctorname",sq.value("doctorname").toString());
//         patientdata[count].insert("medicialrec",sq.value("medicialrec").toString());
//         count++;
//     }
//     return patientdata;
// }

//更改后
QList<QMap<QString, QString>> MyDatabase::checkDataPatient(const QMap<QString, QString>& data)
{
    QString sql = "SELECT * FROM patientusr WHERE name = ? AND  passward= ?;";
    QSqlQuery sq;
    sq.prepare(sql);
    sq.addBindValue(data.value("name"));
    sq.addBindValue(data.value("passward"));
    QList<QMap<QString, QString>> patientdata;

    // 执行查询并遍历结果
    if (sq.exec()) {
        while (sq.next()) {
            QMap<QString, QString> patientRecord;
            patientRecord.insert("id", sq.value("id").toString());
            patientRecord.insert("name", sq.value("name").toString());
            patientRecord.insert("sex", sq.value("sex").toString());
            patientRecord.insert("age", QString::number(sq.value("age").toInt()));
            patientRecord.insert("phone", sq.value("phone").toString());
            patientRecord.insert("address", sq.value("address").toString());
            patientRecord.insert("passward", sq.value("passward").toString());
            patientdata.append(patientRecord);
        }
    } else {
        qWarning() << "Query execution failed:" << sq.lastError().text();
    }

    return patientdata;
}
//原版本
// QMap<QString,QString>* MyDatabase::checkDataDoctor(const QMap<QString,QString>&data)
// {
//     QString sql ="select * from doctorusr where name =? and id=?";
//     QSqlQuery sq;
//     sq.prepare(sql);
//     sq.addBindValue(data.value("name").toStdString().c_str());
//     sq.addBindValue(data.value("id").toStdString().c_str());
//     doctordata =new QMap<QString,QString>[sq.size()];
//     int count=0;
//     //遍历查询语句的结果
//     while(sq.next())
//     {
//         doctordata[count].insert("id",sq.value("id").toString());
//         doctordata[count].insert("name",sq.value("name").toString());
//         doctordata[count].insert("sex",sq.value("sex").toString());
//         doctordata[count].insert("age",sq.value("age").toString());     //可能存在错误
//         doctordata[count].insert("phone",sq.value("phone").toString());
//         doctordata[count].insert("address",sq.value("address").toString());
//         doctordata[count].insert("passward",sq.value("passward").toString());
//         doctordata[count].insert("department",sq.value("departemnt").toString());
//         doctordata[count].insert("departmentid",sq.value("departmentid").toString());
//         doctordata[count].insert("medicialrec",sq.value("medicialrec").toString());
//         doctordata[count].insert("patientname",sq.value("patientname").toString());
//         doctordata[count].insert("patientid",sq.value("patientid").toString());
//         count++;
//     }
//     return doctordata;
// }

//更改后
QList<QMap<QString, QString>> MyDatabase::checkDataDoctor(const QMap<QString, QString>& data)
{
    QString sql = "SELECT * FROM doctorusr WHERE name = ? AND passward = ?";
    QSqlQuery sq;
    sq.prepare(sql);
    sq.addBindValue(data.value("name"));
    sq.addBindValue(data.value("passward"));
    QList<QMap<QString, QString>> doctordata;
    if (sq.exec()) {
        while (sq.next()) {
            QMap<QString, QString> doctorRecord;
            doctorRecord.insert("id", sq.value("id").toString());
            doctorRecord.insert("name", sq.value("name").toString());
            doctorRecord.insert("sex", sq.value("sex").toString());
            doctorRecord.insert("age", QString::number(sq.value("age").toInt()));
            doctorRecord.insert("phone", sq.value("phone").toString());
            doctorRecord.insert("address", sq.value("address").toString());
            doctorRecord.insert("passward", sq.value("passward").toString());
            doctorRecord.insert("departmentname", sq.value("department").toString());
            doctorRecord.insert("departmentid", sq.value("departmentid").toString());

            doctordata.append(doctorRecord);
        }
    } else {
        qWarning() << "Query execution failed:" << sq.lastError().text();
    }

    return doctordata;
}

//原版本
// QMap<QString,QString>* MyDatabase::checkDataDepartment(const QMap<QString,QString> &data)
// {
//     QString sql ="select * from department where departmentname =?";
//     QSqlQuery sq;
//     sq.prepare(sql);
//     sq.addBindValue(data.value("departmentname").toStdString().c_str());
//     sq.addBindValue(data.value("departmentid").toStdString().c_str());
//     departmentdata = new QMap<QString,QString>[sq.size()];
//     int count=0;
//     while(sq.next())
//     {
//         departmentdata[count].insert("departmentname",sq.value("departmentname").toString());
//         departmentdata[count].insert("departmentid",sq.value("departmentid").toString());
//         departmentdata[count].insert("doctorname",sq.value("doctorname").toString());
//         departmentdata[count].insert("doctorid",sq.value("doctorid").toString());
//         count++;
//     }
//     return departmentdata;
// }

//更改后
QList<QMap<QString, QString>> MyDatabase::checkDataDepartment(const QMap<QString, QString>& data)
{
    QString sql = "SELECT * FROM department WHERE departmentname = ? AND departmentid = ?";
    QSqlQuery sq;
    sq.prepare(sql);
    sq.addBindValue(data.value("departmentname"));
    sq.addBindValue(data.value("departmentid"));
    QList<QMap<QString, QString>> departmentdata;
    if (sq.exec()) {
        while (sq.next()) {
            QMap<QString, QString> departmentRecord;
            departmentRecord.insert("departmentname", sq.value("departmentname").toString());
            departmentRecord.insert("departmentid", sq.value("departmentid").toString());
            departmentRecord.insert("doctorname", sq.value("doctorname").toString());
            departmentRecord.insert("doctorid", sq.value("doctorid").toString());

            departmentdata.append(departmentRecord);
        }
    } else {
        qWarning() << "Query execution failed:" << sq.lastError().text();
    }

    return departmentdata;
}

QList<QMap<QString, QString>> MyDatabase::checkDataHealthassessment(const QMap<QString, QString> &data)
{
    QString sql = "SELECT * FROM healthassessment WHERE patientid = ? AND patientname = ?";
    QSqlQuery sq;
    sq.prepare(sql);
    sq.addBindValue(data.value("patientid"));
    sq.addBindValue(data.value("patientname"));
    QList<QMap<QString, QString>> healthassessmentdata;
    if (sq.exec()) {
        while (sq.next()) {
            QMap<QString, QString> departmentRecord;
            departmentRecord.insert("height", QString::number(sq.value("height").toInt()));
            departmentRecord.insert("weight", QString::number(sq.value("weight").toInt()));
            departmentRecord.insert("bloodpressure", QString::number(sq.value("bloodpressure").toInt()));
            departmentRecord.insert("heartrate", QString::number(sq.value("heartrate").toInt()));

            healthassessmentdata.append(departmentRecord);
        }
    } else {
        qWarning() << "Query execution failed:" << sq.lastError().text();
    }

    return healthassessmentdata;
}

QList<QMap<QString, QString>> MyDatabase::checkDataMedicialrec(const QMap<QString, QString> &data)
{
    QString sql = "SELECT medic,doctorname FROM medicialrec WHERE patientid = ? AND patientname = ?";
    QSqlQuery sq;
    sq.prepare(sql);
    sq.addBindValue(data.value("patientid"));
    sq.addBindValue(data.value("patientname"));
    QList<QMap<QString, QString>> medicialrecdata;
    if (sq.exec()) {
        while (sq.next()) {
            QMap<QString, QString> departmentRecord;
            departmentRecord.insert("medic", sq.value("medic").toString());
            departmentRecord.insert("doctorname", sq.value("doctorname").toString());
            medicialrecdata.append(departmentRecord);
        }
    } else {
        qWarning() << "Query execution failed:" << sq.lastError().text();
    }
    return medicialrecdata;

}

QList<QMap<QString, QString>> MyDatabase::checkDataPrescription(const QMap<QString, QString> &data)
{
    QString sql = "SELECT prescr,doctorname FROM prescription WHERE patientid = ? AND patientname = ?";
    QSqlQuery sq;
    sq.prepare(sql);
    sq.addBindValue(data.value("patientid"));
    sq.addBindValue(data.value("patientname"));
    QList<QMap<QString, QString>> prescriptiondata;
    if (sq.exec()) {
        while (sq.next()) {
            QMap<QString, QString> departmentRecord;
            departmentRecord.insert("prescr", sq.value("prescr").toString());
            departmentRecord.insert("doctorname", sq.value("doctorname").toString());
            prescriptiondata.append(departmentRecord);
        }
    } else {
        qWarning() << "Query execution failed:" << sq.lastError().text();
    }
    return prescriptiondata;

}

QList<QMap<QString, QString> > MyDatabase::checkDataRegistration(const QMap<QString, QString> &data)
{
    QString sql = "SELECT patientname,patientid,doctorname,departmentname,date FROM registration WHERE doctorname=?  AND  doctorid = ?";
    QSqlQuery sq;
    sq.prepare(sql);
    sq.addBindValue(data.value("doctorname"));
    sq.addBindValue(data.value("doctorid"));
    QList<QMap<QString, QString>> registrationdata;
    if (sq.exec()) {
        while (sq.next()) {
            QMap<QString, QString> departmentRecord;
            departmentRecord.insert("patientname", sq.value("patientname").toString());
            departmentRecord.insert("patientid", sq.value("patientid").toString());
            departmentRecord.insert("doctorname", sq.value("doctorname").toString());
            departmentRecord.insert("departmentname", sq.value("departmentname").toString());
            departmentRecord.insert("date", sq.value("date").toString());
            registrationdata.append(departmentRecord);
        }
    } else {
        qDebug()<<"registration failed";
        qWarning() << "Query execution failed:" << sq.lastError().text();
    }
    return registrationdata;
}

QList<QMap<QString, QString>> MyDatabase::checkDataDoctorinfo(const QMap<QString, QString> &data)
{
    QString sql = "SELECT id,name,sex,age,phone,address,departmentname FROM doctorusr WHERE name = ?;";
    QSqlQuery sq;
    sq.prepare(sql);
    sq.addBindValue(data.value("doctorname"));
    QList<QMap<QString, QString>> doctordata;
    if (sq.exec()) {
        while (sq.next()) {
            QMap<QString, QString> doctorRecord;
            doctorRecord.insert("id", sq.value("id").toString());
            doctorRecord.insert("name", sq.value("name").toString());
            doctorRecord.insert("sex", sq.value("sex").toString());
            doctorRecord.insert("age", QString::number(sq.value("age").toInt()));
            doctorRecord.insert("phone", sq.value("phone").toString());
            doctorRecord.insert("address", sq.value("address").toString());
            doctorRecord.insert("departmentname", sq.value("department").toString());
            doctordata.append(doctorRecord);
        }
    } else {
        qWarning() << "Query execution failed:" << sq.lastError().text();
    }

    return doctordata;
}

//连接数据库
bool MyDatabase::ConnectDB()
{
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("yiliao.db");
    if(db.open())
    {
        qDebug()<<"连接成功";
    }
    else
    {
        //获得错误信息的封装类
        QSqlError info =  db.lastError();
        QString strerr = info.text();
        qDebug()<<"filed to open DB"<<strerr;
    }
    return true;
}

bool MyDatabase::CreateTable()
{
    //创建患者表
    QString sql = QString("CREATE TABLE patientusr(id TEXT,name TEXT,sex TEXT,"
                          "age INTEGER,phone TEXT,address TEXT,passward TEXT)");
    QSqlQuery sq;
    if(sq.exec(sql))
    {
        qDebug()<<"create table patientusr success";
    }
    else
    {
        qDebug()<<"create table patientusr default";
    }

    //创建患者的病例表
    sql = QString("CREATE TABLE medicialrec(patientid TEXT,patientname TEXT,doctorname TEXT,medic TEXT,date TEXT);");
    if(sq.exec(sql))
    {
        qDebug()<<"create table medicialrec success";
    }
    else
    {
        qDebug()<<"create table medicialrec default";
    }


    //创建医生表
    sql = QString("CREATE TABLE doctorusr(id TEXT,name TEXT,sex TEXT,"
                  "age INTEGER,phone TEXT,address TEXT,"
                  "passward TEXT,departmentname TEXT,"
                  "departmentid TEXT)");
    if(sq.exec(sql))
    {
        qDebug()<<"create table doctorusr success";
    }
    else
    {
        qDebug()<<"create table doctorusr default";
    }

    //创建处方表
    sql = QString("CREATE TABLE prescription(doctorid TEXT,doctorname TEXT,patientid TEXT,patientname TEXT,prescr TEXT,date TEXT)");
    if(sq.exec(sql))
    {
        qDebug()<<"create table prescription success";
    }
    else
    {
        qDebug()<<"create table prescription default";
    }
    //创建科室表
    sql = QString("CREATE TABLE department(departmentname TEXT,departmentid TEXT,"
                  "doctorname TEXT,doctorid TEXT)");
    if(sq.exec(sql))
    {
        qDebug()<<"create table department success";
    }
    else
    {
        qDebug()<<"create table department default";
    }

    //创建健康评估表

    sql = QString("CREATE TABLE healthassessment(patientid TEXT,patientname TEXT,height INTEGER,weight INTEGER,bloodpressure INTEGER,heartrate INTEGER)");
    if(sq.exec(sql))
    {
        qDebug()<<"create table healthassessment success";
    }
    else
    {
        qDebug()<<"create table healthassessment default";
    }
    //创建挂号表

    sql = QString("CREATE TABLE registration(patientid TEXT,patientname TEXT,departmentname TEXT,doctorname TEXT,doctorid TEXT,date TEXT)");
    if(sq.exec(sql))
    {
        qDebug()<<"create table registration success";
    }
    else
    {
        qDebug()<<"create table registration default";
    }
    return true;
}


//转换函数
//QMap 转化为 QByeteArray
QByteArray MyDatabase::mapToByteArrayCustom(const QMap<QString, QString>& map)
{
    QStringList pairs;
    for (auto it = map.constBegin(); it != map.constEnd(); ++it) {
        pairs.append(it.key() + "=" + it.value());
    }
    QString result = pairs.join("&");  // 例如使用 '&' 连接键值对
    return result.toUtf8();  // 转换为 QByteArray
}

//QByeteArray 转化为 QMap;
QMap<QString, QString> MyDatabase::byteArrayToMapCustom(const QByteArray& byteArray)
{
    QMap<QString, QString> map;
    QStringList pairs = QString(byteArray).split('&'); // 假设使用 & 分隔
    for (const QString& pair : pairs) {
        QStringList keyValue = pair.split('=');
        if (keyValue.size() == 2) {
            map.insert(keyValue[0], keyValue[1]);
        }
    }
    return map;
}

//QMap数组转化成 QByteArray
QByteArray MyDatabase::convertQMapArrayToByteArrayCustom(const QList<QMap<QString, QString> > &mapArray)
{
    QByteArray byteArray;

    for (const QMap<QString, QString>& map : mapArray) {
        QList<QByteArray> mapPairs;

        for (auto it = map.constBegin(); it != map.constEnd(); ++it) {
            mapPairs.append(it.key().toUtf8() + "=" + it.value().toUtf8());
        }

        byteArray.append(mapPairs.join("&"));  // 使用 "&" 分隔键值对
        byteArray.append(";");  // 使用 ";" 分隔每个 QMap
    }
    return byteArray;
}

//将QByteArray转化为QMap数组
QList<QMap<QString, QString> > MyDatabase::convertByteArrayToQMapArrayCustom(const QByteArray &byteArray)
{
    QList<QMap<QString, QString>> mapArray;

    // 使用 ";" 分割每个 QMap 的数据
    QList<QByteArray> mapStrings = byteArray.split(';');
    for (const QByteArray& mapString : mapStrings) {
        if (mapString.isEmpty()) continue;  // 跳过空字符串

        QMap<QString, QString> map;
        // 使用 "&" 分割每个键值对
        QList<QByteArray> pairs = mapString.split('&');
        for (const QByteArray& pair : pairs) {
            QList<QByteArray> keyValue = pair.split('=');
            if (keyValue.size() == 2) {
                map.insert(QString::fromUtf8(keyValue[0]), QString::fromUtf8(keyValue[1]));
            }
        }
        mapArray.append(map);
    }

    return mapArray;
}
